package fr.esiag.isidis.fi.pdsTeam.tram;

import java.util.Date;

import fr.esiag.isidis.fi.pdsTeam.events.RawEvent;

public class TramManager {
	static TramManager instance=null;
	
	private TramManager() {
	}
	
	public static TramManager getInstance() {
		if(instance==null) instance=new TramManager();
		return instance;
	}
	
	public static byte[] generateTram (String sensorMacAddress,int codeAlerte){
		byte[]mac = sensorMacAddress.getBytes();
		
		byte[] tram = new byte[mac.length+2];
		tram[0] = (byte)codeAlerte;
		tram[1] = (byte)sensorMacAddress.length();
		for (int i = 0; i < mac.length; i++) {
			tram[i+2] = mac[i];
		}
		System.out.println("code: "+codeAlerte);
		//extractTram(tram);
		return tram;
	}
	
	@SuppressWarnings("deprecation")
	public static RawEvent extractTram(byte[] tram, Date now){
			
			int codeAlerte = new Integer(tram[0]);
			int macLength = new Integer(tram[1]);
			
			/*System.out.println("code:   "+codeAlerte);
			System.out.println("length: "+macLength);*/
			
			byte[]macByteArray = new byte[macLength];
			
			for (int i = 2; i < macLength+2; i++) {
				macByteArray[i-2] = tram[i];
			}
			
			//System.out.println("chaine extraite: "+new String(macByteArray));
			
			RawEvent ev = new RawEvent(0, new String(macByteArray), codeAlerte);
			ev.setReceptionTime(now.toGMTString());
			
			//System.out.println(ev.toString());
			return ev;
		}
	
}
